await @promise("lab.ready");
require("./annotation/index.js");

const slime_member_sources = @options("sources") || {};

@lab.export.error("./error");
@lab.export.constant("./constant");

@lab.export.service("slime@", "./service");

@.async(function() {

    @celebr("slime system start to init");
    @lab.export.model("./model").pipe(this);

}).then(function() {
    
    @lab.export.model({
        "slime_member_ids": @.merge.simple({
            "id": @lab.types.id(),          // member_id
            "main_id": @lab.types.string(), // member_id 这个就是主要member_id
            "ouid"   : @lab.types.string(), // member_id 这个就是主要member_id
            "last_update": @lab.types.date(),
            "uid": @lab.types.string(),     // 为会员绑定后台登陆用户
            "phone": @lab.types.string(),   // 手机号,
            "email": @lab.types.string(),   // 手机号
        }, @.keys(slime_member_sources).reduce(function(result, source) {
            
            if (["email","phone"].indexOf(source) < 0) {
                result[source.split(".").join("_") + "_id"] = @lab.types.string();
            }
            
            return result;
        }, {}))
    }).pipe(this);

}).then(function() {

    @celebr("slime system model init complete");
    @lab.snapshot(function(snapshot) {

        return @.async(function() {
            snapshot.slime.core.@init(@options("core") || {}).pipe(this);
        })

    }).pipe(this);

}).then(function(){

    @celebr("slime core system init complete");

    @.async(function () {
        
        @slime.event({
            name  : "member_accessed",
            alias : "会员访问",
            trigger : "slime.member_accessed"
        }).pipe(this);

    }).pipe(this);
    
    
}).then(function () {

    @lab(function(lab){
        return @.async(async function () {
            lab.slime.core.@autoRegisterExcelTemplate("slime.store",@path(__dirname,"library","store.xlsx"));
            lab.slime.core.@autoRegisterExcelTemplate("slime.brand_sku",@path(__dirname,"library","brand_sku.xlsx"));
            lab.slime.core.@autoRegisterExcelTemplate("slime.member",@path(__dirname,"library","member.xlsx"));    
        })
    }).pipe(this);
    
}).finished(function(error) {

    if (error) {
        @error(error);
    } else {
        @celebr("slime system init complete");
    }

    @mew.emit("slime.ready");
})